import base64
import datetime
from typing import List
from pydantic import Field, BaseModel, root_validator
from package.fastapi.schema import Page, BatchId


# ----------------------------------------个人信息----------------------------------------------------
class Password(BaseModel):
    password: str = Field(..., title='密码')


class EditOwner(BaseModel):
    """修改个人信息"""
    alias: str = Field(None, title='显示名')
    extra: dict = Field(..., title='额外属性')


class EditPassword(Password):
    """修改密码"""
    old: str = Field(..., title='密码')


class Authorization(BaseModel):
    """修改密码"""
    expired_at: int = Field(None, title='过期时间')
    description: str = Field('', title='授权描述')


class AuthorizationOut(Authorization):
    """修改密码"""
    id: str = Field(..., title='记录ID')
    expired_at: int = Field(None, title='')
    created_at: datetime.datetime = Field(None, title='')

    class Config:
        orm_mode = True
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


# -----------------------------------用户----------------------------------------------------
class EditUser(BaseModel):
    """修改记录"""
    alias: str = Field(None, title='别名')
    extra: dict = Field(..., title='额外属性')


class CreateUser(EditUser, Password):
    """创建记录"""
    name: str = Field(..., title='账号')


class UserOut(CreateUser):
    """返回数据不返回密码"""
    id: str = Field(..., title='记录ID')
    enabled: bool = Field(..., title='是否启用')
    created_at: datetime.datetime = Field(None, title='')

    class Config:
        orm_mode = True
        extra = 'allow'
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class OwenerOut(CreateUser):
    """返回数据不返回密码"""
    id: str = Field(..., title='记录ID')
    authorization: List[AuthorizationOut] = Field(None, title='记录ID')
    enabled: bool = Field(..., title='是否启用')
    created_at: datetime.datetime = Field(None, title='')

    class Config:
        orm_mode = True
        extra = 'allow'
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class PageUsers(Page):
    """获取页码条目"""
    data: List[UserOut] = Field(..., title='')


class EnableUser(BaseModel):
    """修改密码"""
    enabled: bool = Field(..., title='密码')


# -------------------------------------角色管理----------------------------------------------------------
class Permit(BaseModel):
    module: str = Field(..., title='模块')
    visible: bool = Field(..., title='模块是否显示')
    sub_module: List[dict] = Field(..., title='子模块功能函数')


class EditRole(BaseModel):
    """修改记录"""
    alias: str = Field(None, title='显示角色名')
    description: str = Field(None, title='角色描述信息')
    permit: List[Permit] = Field(None, title='允许的权限')


class CreateRole(EditRole):
    """创建记录"""
    name: str = Field(..., title='角色名')


class RoleOut(CreateRole):
    id: str = Field(..., title='记录ID')
    created_at: datetime.datetime = Field(None, title='')

    class Config:
        orm_mode = True
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class PageRoles(Page):
    """获取页码条目"""
    data: List[RoleOut] = Field(..., title='')


class UserRoleOut(UserOut):
    """返回数据不返回密码"""
    roles: List[RoleOut] = Field(..., title='')

    class Config:
        orm_mode = True
        extra = 'allow'
